﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using nERP.Model.Wms;
using nERP.Model;
using nERP.Model.Bas;
using nERP.Model.Po;

namespace nERP.ViewModel.Wms.BuyInWarehouseDetailVMs
{
    public partial class BuyInWarehouseDetailListVM : BasePagedListVM<BuyInWarehouseDetail_View, BuyInWarehouseDetailSearcher>
    {

        protected override IEnumerable<IGridColumn<BuyInWarehouseDetail_View>> InitGridHeader()
        {
            return new List<GridColumn<BuyInWarehouseDetail_View>>{

                this.MakeGridHeader(x => x.BuyInWarehouseDetail_Seq).SetTitle(@Localizer["Page.序号"].Value),
                this.MakeGridHeader(x => x.BuyInWarehouseDetail_BuyInWarehouse).SetTitle(@Localizer["Page.入库单号"].Value),
                this.MakeGridHeader(x => x.BuyInWarehouseDetail_BuyInWarehouseDetailCode).SetTitle(@Localizer["Page.入库明细单号"].Value),
                this.MakeGridHeader(x => x.BuyInWarehouseDetail_Warehouse).SetTitle(@Localizer["Page.仓库"].Value),
                this.MakeGridHeader(x => x.BuyInWarehouseDetail_Buy).SetTitle(@Localizer["Page.采购订单"].Value),
                this.MakeGridHeader(x => x.BuyInWarehouseDetail_BuyDetail).SetTitle(@Localizer["Page.采购明细"].Value),
                this.MakeGridHeader(x => x.BuyInWarehouseDetail_Goods).SetTitle(@Localizer["Page.商品"].Value),
                this.MakeGridHeader(x => x.BuyInWarehouseDetail_GoodsName).SetTitle(@Localizer["Page.商品名称"].Value),
                this.MakeGridHeader(x => x.BuyInWarehouseDetail_GoodsSpec).SetTitle(@Localizer["Page.商品规格"].Value),
                this.MakeGridHeader(x => x.BuyInWarehouseDetail_GoodsUnit).SetTitle(@Localizer["Page.计量单位"].Value),
                this.MakeGridHeader(x => x.BuyInWarehouseDetail_BuyInNumber).SetTitle(@Localizer["Page.入库数量"].Value),

                this.MakeGridHeader(x => x.BuyDetail_UnitPrice).SetTitle(@Localizer["Page.购货单价"].Value),
                this.MakeGridHeader(x => x.BuyDetail_DiscountPrice).SetTitle(@Localizer["Page.折扣价格"].Value),
                this.MakeGridHeader(x => x.BuyDetail_TotalPrice).SetTitle(@Localizer["Page.金额"].Value),
                this.MakeGridHeader(x => x.BuyDetail_TaxRate).SetTitle(@Localizer["Page.税率"].Value),
                this.MakeGridHeader(x => x.BuyDetail_TaxUnitPrice).SetTitle(@Localizer["Page.含税单价"].Value),
                this.MakeGridHeader(x => x.BuyDetail_TaxPrice).SetTitle(@Localizer["Page.税额"].Value),
                this.MakeGridHeader(x => x.BuyDetail_TotalTaxPrice).SetTitle(@Localizer["Page.含税金额"].Value),

                this.MakeGridHeader(x => x.BuyInWarehouseDetail_Remark).SetTitle(@Localizer["_Admin.Remark"].Value),
                this.MakeGridHeader(x => x.BuyInWarehouseDetail_Status).SetTitle(@Localizer["Page.状态"].Value),
                this.MakeGridHeader(x => x.BuyInWarehouseDetail_CreateTime).SetTitle(@Localizer["_Admin.CreateTime"].Value),
                this.MakeGridHeader(x => x.BuyInWarehouseDetail_UpdateTime).SetTitle(@Localizer["_Admin.UpdateTime"].Value),
                this.MakeGridHeader(x => x.BuyInWarehouseDetail_CreateBy).SetTitle(@Localizer["_Admin.CreateBy"].Value),
                this.MakeGridHeader(x => x.BuyInWarehouseDetail_UpdateBy).SetTitle(@Localizer["_Admin.UpdateBy"].Value),

                this.MakeGridHeaderAction(width: 200)
            };
        }


        public override IOrderedQueryable<BuyInWarehouseDetail_View> GetSearchQuery()
        {
            var query = DC.Set<BuyInWarehouseDetail>()

                .CheckEqual(Searcher.Seq, x => x.Seq)
                .CheckEqual(Searcher.BuyInWarehouseId, x => x.BuyInWarehouseId)
                .CheckContain(Searcher.BuyInWarehouseDetailCode, x => x.BuyInWarehouseDetailCode)
                .CheckEqual(Searcher.WarehouseId, x => x.WarehouseId)
                .CheckEqual(Searcher.BuyId, x => x.BuyId)
                .CheckEqual(Searcher.BuyDetailId, x => x.BuyDetailId)
                .CheckEqual(Searcher.GoodsId, x => x.GoodsId)
                .CheckContain(Searcher.GoodsName, x => x.GoodsName)
                .CheckContain(Searcher.GoodsSpec, x => x.GoodsSpec)
                .CheckContain(Searcher.GoodsUnit, x => x.GoodsUnit)
                .CheckEqual(Searcher.BuyInNumber, x => x.BuyInNumber)
                .CheckContain(Searcher.Remark, x => x.Remark)
                .CheckEqual(Searcher.Status, x => x.Status)
                .CheckBetween(Searcher.CreateTime?.GetStartTime(), Searcher.CreateTime?.GetEndTime(), x => x.CreateTime, includeMax: false)
                .CheckBetween(Searcher.UpdateTime?.GetStartTime(), Searcher.UpdateTime?.GetEndTime(), x => x.UpdateTime, includeMax: false)
                .CheckContain(Searcher.CreateBy, x => x.CreateBy)
                .CheckContain(Searcher.UpdateBy, x => x.UpdateBy)
                .Select(x => new BuyInWarehouseDetail_View
                {
                    ID = x.ID,

                    BuyInWarehouseDetail_Seq = x.Seq,
                    BuyInWarehouseDetail_BuyInWarehouse = DC.Set<BuyInWarehouse>().Where(z0 => z0.BuyInWarehouseCode == x.BuyInWarehouseId).Select(y => y.BuyInWarehouseCode).FirstOrDefault(),
                    BuyInWarehouseDetail_BuyInWarehouseDetailCode = x.BuyInWarehouseDetailCode,
                    BuyInWarehouseDetail_Warehouse = DC.Set<Warehouse>().Where(z0 => z0.WarehouseCode == x.WarehouseId).Select(y => y.WarehouseName).FirstOrDefault(),
                    BuyInWarehouseDetail_Buy = DC.Set<Buy>().Where(z0 => z0.BuyCode == x.BuyId).Select(y => y.BuyCode).FirstOrDefault(),
                    BuyInWarehouseDetail_BuyDetail = DC.Set<BuyDetail>().Where(z0 => z0.BuyDetailCode == x.BuyDetailId).Select(y => y.BuyDetailCode).FirstOrDefault(),
                    BuyInWarehouseDetail_Goods = x.GoodsId,//DC.Set<Goods>().Where(z0 => z0.GoodsCode == x.GoodsId).Select(y => y.GoodsName).FirstOrDefault(),
                    BuyInWarehouseDetail_GoodsName = x.GoodsName,
                    BuyInWarehouseDetail_GoodsSpec = x.GoodsSpec,
                    BuyInWarehouseDetail_GoodsUnit = x.GoodsUnit,
                    BuyInWarehouseDetail_BuyInNumber = x.BuyInNumber,

                    BuyDetail_UnitPrice = DC.Set<BuyDetail>().Where(z0 => z0.BuyDetailCode == x.BuyDetailId).Select(y => y.UnitPrice).FirstOrDefault(),
                    BuyDetail_DiscountPrice = DC.Set<BuyDetail>().Where(z0 => z0.BuyDetailCode == x.BuyDetailId).Select(y => y.DiscountPrice).FirstOrDefault(),
                    BuyDetail_TotalPrice = DC.Set<BuyDetail>().Where(z0 => z0.BuyDetailCode == x.BuyDetailId).Select(y => y.TotalPrice).FirstOrDefault(),
                    BuyDetail_TaxRate = DC.Set<BuyDetail>().Where(z0 => z0.BuyDetailCode == x.BuyDetailId).Select(y => y.TaxRate).FirstOrDefault(),
                    BuyDetail_TaxUnitPrice = DC.Set<BuyDetail>().Where(z0 => z0.BuyDetailCode == x.BuyDetailId).Select(y => y.TaxUnitPrice).FirstOrDefault(),
                    BuyDetail_TaxPrice = DC.Set<BuyDetail>().Where(z0 => z0.BuyDetailCode == x.BuyDetailId).Select(y => y.TaxPrice).FirstOrDefault(),
                    BuyDetail_TotalTaxPrice = DC.Set<BuyDetail>().Where(z0 => z0.BuyDetailCode == x.BuyDetailId).Select(y => y.TotalTaxPrice).FirstOrDefault(),

                    BuyInWarehouseDetail_Remark = x.Remark,
                    BuyInWarehouseDetail_Status = x.Status,
                    BuyInWarehouseDetail_CreateTime = x.CreateTime,
                    BuyInWarehouseDetail_UpdateTime = x.UpdateTime,
                    BuyInWarehouseDetail_CreateBy = x.CreateBy,
                    BuyInWarehouseDetail_UpdateBy = x.UpdateBy,
                })
                .OrderBy(x => x.BuyInWarehouseDetail_Seq);
            return query;
        }


    }
    public class BuyInWarehouseDetail_View : BuyInWarehouseDetail
    {

        public int? BuyInWarehouseDetail_Seq { get; set; }
        public string BuyInWarehouseDetail_BuyInWarehouse { get; set; }
        public string BuyInWarehouseDetail_BuyInWarehouseDetailCode { get; set; }
        public string BuyInWarehouseDetail_Warehouse { get; set; }
        public string BuyInWarehouseDetail_Buy { get; set; }
        public string BuyInWarehouseDetail_BuyDetail { get; set; }
        public string BuyInWarehouseDetail_Goods { get; set; }
        public string BuyInWarehouseDetail_GoodsName { get; set; }
        public string BuyInWarehouseDetail_GoodsSpec { get; set; }
        public string BuyInWarehouseDetail_GoodsUnit { get; set; }
        public decimal? BuyInWarehouseDetail_BuyInNumber { get; set; }
        public decimal? BuyDetail_UnitPrice { get; set; }
        public decimal? BuyDetail_DiscountPrice { get; set; }
        public decimal? BuyDetail_TotalPrice { get; set; }
        public decimal? BuyDetail_TaxRate { get; set; }
        public decimal? BuyDetail_TaxUnitPrice { get; set; }
        public decimal? BuyDetail_TaxPrice { get; set; }
        public decimal? BuyDetail_TotalTaxPrice { get; set; }
        public string BuyInWarehouseDetail_Remark { get; set; }
        public dStatusTypeEnum? BuyInWarehouseDetail_Status { get; set; }
        public DateTime? BuyInWarehouseDetail_CreateTime { get; set; }
        public DateTime? BuyInWarehouseDetail_UpdateTime { get; set; }
        public string BuyInWarehouseDetail_CreateBy { get; set; }
        public string BuyInWarehouseDetail_UpdateBy { get; set; }

    }

}